<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="宗地序号" prop="id">
        <el-input
          v-model="queryParams.id"
          placeholder="请输入宗地序号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="宗地编号" prop="landNo">
        <el-input
          v-model="queryParams.landNo"
          placeholder="请输入宗地编号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="土地证号" prop="landCertificate">
        <el-input
          v-model="queryParams.landCertificate"
          placeholder="请输入土地证号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="宗地坐落" prop="landLocation">
        <el-input
          v-model="queryParams.landLocation"
          placeholder="请输入宗地坐落"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <!-- <el-form-item label="所属区县" prop="landDistrict">
        <el-input
          v-model="queryParams.landDistrict"
          placeholder="请输入所属区县"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="所属乡镇" prop="landTownship">
        <el-input
          v-model="queryParams.landTownship"
          placeholder="请输入所属乡镇"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="所属村" prop="landVillage">
        <el-input
          v-model="queryParams.landVillage"
          placeholder="请输入所属村"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="面积" prop="landArea">
        <el-input
          v-model="queryParams.landArea"
          placeholder="请输入面积"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="征收标准（元/平方米*年）" prop="levyStandards">
        <el-input
          v-model="queryParams.levyStandards"
          placeholder="请输入征收标准（元/平方米*年）"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item> -->
      <el-form-item label="权利性质" prop="landQlxz">
        <el-input
          v-model="queryParams.landQlxz"
          placeholder="请输入权利性质"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="批准用途" prop="landApprovedUse">
        <el-input
          v-model="queryParams.landApprovedUse"
          placeholder="请输入批准用途"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="实际用途" prop="landActualUse">
        <el-input
          v-model="queryParams.landActualUse"
          placeholder="请输入实际用途"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <!-- <el-form-item label="土地级别" prop="landLevel">
        <el-input
          v-model="queryParams.landLevel"
          placeholder="请输入土地级别"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item> -->
      <el-form-item label="不动产单元号" prop="landBdcdyh">
        <el-input
          v-model="queryParams.landBdcdyh"
          placeholder="请输入不动产单元号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable class="!w-240px">
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.SYS_STATUS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="创建者" prop="creator">
        <el-input
          v-model="queryParams.creator"
          placeholder="请输入创建者"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
          v-model="queryParams.createTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['zszx:land:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['zszx:land:export']"
        >
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <!-- 子表的列表 -->
      <el-table-column type="expand">
        <template #default="scope">
          <el-tabs model-value="landFile">
            <el-tab-pane label="地块档案" name="landFile">
              <LandFileList :belong-id="scope.row.id" />
            </el-tab-pane>
            <el-tab-pane label="权利人信息" name="obligee">
              <ObligeeList :land-id="scope.row.id" />
            </el-tab-pane>
          </el-tabs>
        </template>
      </el-table-column>
      <el-table-column label="宗地序号" align="center" prop="id" width="60">
        <template #default="scope">
          <el-link type="primary" @click="openForm('update', scope.row.id)">{{
            scope.row.id
          }}</el-link>
        </template>
      </el-table-column>
      <el-table-column label="宗地编号" align="center" prop="landNo" width="200" />
      <el-table-column label="土地证号" align="center" prop="landCertificate" width="200" />
      <el-table-column label="宗地坐落" align="center" prop="landLocation" width="180"/>
      <!-- <el-table-column label="所属区县" align="center" prop="landDistrict" />
      <el-table-column label="所属乡镇" align="center" prop="landTownship" />
      <el-table-column label="所属村" align="center" prop="landVillage" /> -->
      <el-table-column label="面积" align="center" prop="landArea" />
      <el-table-column label="征收标准（元/平方米*年）" align="center" prop="levyStandards" />
      <el-table-column label="权利性质" align="center" prop="landQlxz" />
      <el-table-column label="批准用途" align="center" prop="landApprovedUse" />
      <el-table-column label="实际用途" align="center" prop="landActualUse" />
      <el-table-column label="土地级别" align="center" prop="landLevel" />
      <el-table-column label="不动产单元号" align="center" prop="landBdcdyh" />
      <!-- <el-table-column label="坐标" align="center" prop="landCoordinate" />
      <el-table-column label="备注" align="center" prop="remark" /> -->
      <el-table-column label="状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.SYS_STATUS" :value="scope.row.status" />
        </template>
      </el-table-column>
      <!-- <el-table-column label="创建者" align="center" prop="creator" />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="更新者" align="center" prop="updater" />
      <el-table-column
        label="更新时间"
        align="center"
        prop="updateTime"
        :formatter="dateFormatter"
        width="180px"
      /> -->
      <el-table-column label="操作" align="center" min-width="120px">
        <template #default="scope">
          <el-button
            link
            type="primary"
            v-show="scope.row.landCoordinate"
            @click="handleAim(scope.row)"
          >
            定位
          </el-button>
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['zszx:land:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['zszx:land:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <LandForm ref="formRef" @success="getList" />

  <!-- 地图定位对话框-->
  <el-dialog title="定位查看" v-model="mapOpen" ref="mapDialog" width="1200px" append-to-body>
    <div style="position: relative; height: 80vh; border: 1px solid rgb(0 204 255)">
      <Map ref="arcgisMap" v-show="mapOpen" />
    </div>
  </el-dialog>
</template>

<script setup lang="ts">
import { getStrDictOptions, DICT_TYPE } from '@/utils/dict'
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { LandApi, LandVO } from '@/api/zszx/land'
import LandForm from './LandForm.vue'
import LandFileList from './components/LandFileList.vue'
import ObligeeList from './components/ObligeeList.vue'

import Map from '@/views/zszx/arcgis/Map.vue'
const { proxy } = getCurrentInstance()

/** 宗地信息 列表 */
defineOptions({ name: 'Land' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<LandVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  id: undefined,
  landNo: undefined,
  landCertificate: undefined,
  landLocation: undefined,
  landDistrict: undefined,
  landTownship: undefined,
  landVillage: undefined,
  landArea: [],
  levyStandards: [],
  landQlxz: undefined,
  landApprovedUse: undefined,
  landActualUse: undefined,
  landLevel: undefined,
  landBdcdyh: [],
  status: undefined,
  creator: undefined,
  createTime: []
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

const mapOpen = ref(false)

/** 定位按钮操作 */
function handleAim(row) {
  // console.log("---",row.landCoordinate)
  if (row.landCoordinate && row.landCoordinate.trim() != '') {
    mapOpen.value = true
    nextTick(() => {
      // console.log('定位中')
      proxy.$refs.arcgisMap.gotoExtent(row)
    })
  } else {
    proxy.$modal.msgError('地块无坐标，无法定位!')
  }
}
/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await LandApi.getLandPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await LandApi.deleteLand(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await LandApi.exportLand(queryParams)
    download.excel(data, '宗地信息.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
